package cn.itsource.user.service.impl;

import cn.itsource.basic.constant.UserConstants;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.dto.UserDto;
import cn.itsource.user.mapper.LogininfoMapper;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import javafx.scene.chart.BubbleChart;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.jws.Oneway;
import java.util.Date;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void add(User user) {
        super.add(user);
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }

    @Override
    @Transactional
    public void phoneRegister(UserDto userDto) {
        //1.校验数据
        String phone = userDto.getPhone();
        //1.1 参数不能为空
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(userDto.getVerifyCode()) ||
            StringUtils.isEmpty(userDto.getPassword()) ||  StringUtils.isEmpty(userDto.getConfirmPwd())){
            throw new BusinessException("请输入完整信息！！");
        }
        //1.2 手机不能被注册  查询user
        User userTmp = userMapper.loadByPhone(phone);
        if (userTmp != null) {
            throw new BusinessException("用户已被注册，请直接登录，如果忘记，请找回密码！");
        }
        //1.3 两次密码是否一致
        if(!userDto.getPassword().equals(userDto.getConfirmPwd())){
            throw new BusinessException("两次密码不一致！");
        }
        Object codeObj = redisTemplate.opsForValue().get(UserConstants.USER_REGISTER_CODE_PREFIX + phone);
        //2 验证码校验  不要区分大小写
        if(codeObj == null){
            throw new BusinessException("验证码已失效，请重新获取！");
        }
        String code = ((String)codeObj).split(":")[0];
        if(!code.equalsIgnoreCase(userDto.getVerifyCode())){
            throw new BusinessException("验证码输入错误！");
        }
        //3 注册
        //3.1 登录信息 和 用户

        User user = UserDto2User(userDto);
        Logininfo logininfo = user2LoginInfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private User UserDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //生成随机32位的salt
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    private Logininfo user2LoginInfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }


}
